package com.samsung.spen.sshare.explorer.util;

import java.io.File;

public class FileExtend implements Comparable<FileExtend> {

	File file;
	public boolean flag_choosed = false;

	public FileExtend(File file) {
		this.file = file;
	}

	public FileExtend(String file) {
		this.file = new File(file);
	}

	public File getFile() {
		return file;
	}
	
	@Override
	public String toString() {

		String string = "Name: " + getFile().getName() + ", size (byte): "
				+ getFile().length();
		return string;
	}

	@Override
	public int compareTo(FileExtend another) {

		switch (SConfig.FLAG_SORT) {

		case SConfig.FLAG_SORT_BY_NAME:
			return file.getName().compareToIgnoreCase(
					another.getFile().getName());

		case SConfig.FLAG_SORT_BY_SIZE:

			if (file.isDirectory()) {

				if (another.getFile().isDirectory()) {

					return file.getName().compareToIgnoreCase(
							another.getFile().getName());

				} else {
					return 1;
				}

			} else {

				if (another.getFile().isDirectory()) {
					return -1;
				}

				else {
					return (file.length() - another.getFile().length()) > 0 ? 1
							: -1;
				}
			}

		default:
			break;
		}
		
		return 0;
	}
	
	public static float getLeng(File file) {

		float leng = 0;

		if (file.isDirectory()) {

			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {

				if (!files[i].isDirectory()) {

					leng += files[i].length();
				} else {

					leng += getLeng(files[i]);
				}
			}
		} else {

			leng = file.length();
		}

		return leng;
	}

	public String getSize() {

		long sizeFile = getFile().length();
		long devide = 1;
		String unit = "Kb";

		if (sizeFile > 1E11) {
			devide = 1000000;
			unit = "GB";

		} else if (sizeFile > 1E8) {

			devide = 1000;
			unit = "MB";

		} else if (sizeFile > 1E5) {

			devide = 1000;
			unit = "KB";
		}

		return Integer.valueOf(String.valueOf(sizeFile / devide)) + unit;
	}

}
